const app = getApp();
const log =require('../../log.js');
const util=require('../../util.js');
const tabService = require("../../libs/tabService");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        sid:1,
        sname:'',
        brand_id:0,
        series_id:0,
        color_id:0,
        motor_id:0,
        cabin_id:0,
        box_id:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this ;
        let d =   app.globalData;  
        that.setData({
            platform:d.platform,    
           // userInfo:userInfo,
             width:d.windowWidth,
            height:d.windowHeight,
            statusBarHeight:d.statusBarHeight,
            navHeight:d.navHeight,
            marginTop:d.navTitleTop,
            domain:d.domain,
            uploadUrl:d.uploadUrl
          })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    Index(){
        let that = this;
        var userInfo =  wx.getStorageSync('userInfo');
        var data = [];
        data['user_id']= userInfo.id;
        var url = 'clear/getIndex';

      app.request(url,data,'get').then(res=>{ 
        console.log('customer getIndex'); 
         console.log(res);
     
         //this.initChart(res.pie);
         that.setData({
             'cabin':res.cabin,
             'motor':res.motor,
             'brand':res.brand,
             'color':res.color,
             'series':res.series,
             'list':res.list,
             'box':res.box,
             'adimg':res.adimg,
             'count':res.count,
             'title':res.title
            
             
         });

       //  that.wxUpdate(); //that.wxLogIn();  
      })

      },
      getHeight:function(e){
        var winWid = wx.getSystemInfoSync().windowWidth;//获取当前屏幕的宽度
        var imgh = e.detail.height;//图片高度
        var imgw = e.detail.width;
        var sH = winWid * imgh / imgw + "px"
        this.setData({
            swiperH: sH//设置高度
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {    
       
        //this.getIndex();
       this.Index();
        var role = wx.getStorageSync('role');
       console.log(role);
       this.update(role);
        var tabIndex;
        if(role==0){
          tabIndex=1;
        }else if(role==1){
          tabIndex=2;
        }else{
          tabIndex=2;
        }
           
        tabService.updateIndex(this, tabIndex);
      },
      update(role) {
        tabService.updateRole(this, role);
      },
      goto:function(e){
        console.log(e);
        var url=e.currentTarget.dataset.url;
        var index=e.currentTarget.dataset.index;
        var parent_id=wx.getStorageSync('parent_id');
        app.goto(url,index);
    },
    goBack:function(){
        wx.navigateBack();
      },
      goIndex:function(){
        wx.reLaunch({
          url: '../index/index',
        })
      },
      getSeries(e){
          console.log(e);
          var d = e.currentTarget.dataset;

          var series = this.data.series; 
          var id = d.id;
          var name = d.name;
          var data = [];
          data['id']= id;
          var url = 'clear/getSeries';
        let that = this;
        app.request(url,data,'get').then(res=>{ 
          console.log('clear index getSeries'); 
           console.log(res);
           var len = res.len;
           var series = res.series;
           var height = 74+41*len;
          that.setData({
              brand_id:id,
              brand_name:name,
              showSeries:1,
              needH:height,
              series:series
          })
        });
          
      
          
      },
      setSeries(e){
          console.log(e);
          var d = e.currentTarget.dataset;
          var id = d.id;
          var name = d.name;
          let v = this.data; 
          if(id == 0){
            name = v.brand_name+"全系";
        }  
          this.search(v.brand_id,id,v.color_id,v.motor_id,v.cabin_id,v.box_id);
          this.setData({
            showSeries:0,
            series_id:id,
            series_name:name,
           // needH:height
        })
      },
      setSearch(e){
          console.log(e);
          var d = e.currentTarget.dataset;
          var id = d.id;
          //var name = d.name
          this.setData({
              sid:id,
          })
      },
      setColor(e){
        console.log(e);
        var d = e.currentTarget.dataset;
        var id = d.id;
        var name = d.name;
    
        let v = this.data;     
        this.search(v.brand_id,v.series_id,id,v.motor_id,v.cabin_id,v.box_id);
        this.setData({
            color_id:id,
            color_name:name
        })
      },
      setMotor(e){
        console.log(e);
        var d = e.currentTarget.dataset;
        var id = d.id;
        var name = d.name;
        let v = this.data;     
        this.search(v.brand_id,v.series_id,v.color_id,id,v.cabin_id,v.box_id);
        this.setData({
            motor_id:id,
            motor_name:name
        })
      },
      setCabin(e){
        console.log(e);
        var d = e.currentTarget.dataset;
        var id = d.id;
        var name = d.name;
        let v = this.data;     
        this.search(v.brand_id,v.series_id,v.color_id,v.motor_id,id,v.box_id);
        this.setData({
            cabin_id:id,
            cabin_name:name
        })
      },
      setBox(e){
        console.log(e);
        var d = e.currentTarget.dataset;
        var id = d.id;
        var name = d.name;
        let v = this.data;     
        this.search(v.brand_id,v.series_id,v.color_id,v.motor_id,v.cabin_id,id);
        this.setData({
            box_id:id,
            box_name:name
        })
      },
      delBox(){
        let d = this.data;     
        this.search(d.brand_id,d.series_id,d.color_id,d.motor_id,d.cabin_id,0);
        this.setData({
            box_id:0,
            box_name:''
        })
      },
      delCabin(){
        let d = this.data;     
        this.search(d.brand_id,d.series_id,d.color_id,d.motor_id,0,d.box_id);
        this.setData({
            cabin_id:'',
            cabin_name:''
        })
      },
      delMotor(){
        let d = this.data;     
        this.search(d.brand_id,d.series_id,d.color_id,0,d.cabin_id,d.box_id);
        this.setData({
            motor_id:'',
            motor_name:''
        })
      },
      delColor(){
        let d = this.data;     
        this.search(d.brand_id,d.series_id,0,d.motor_id,d.cabin_id,d.box_id);
        this.setData({
            color_id:'',
            color_name:''
        })
      },
      delSeries(){
        let d = this.data;          
        this.search(0,0,d.color_id,d.motor_id,d.cabin_id,d.box_id);
        this.setData({
            series_id:'',
            brand_id:0,
            brand_name:'',
            series_name:''
        })
      },
      topSearch(e){

      },
      search(brand_id,series_id,color_id,motor_id,cabin_id,box_id){
        let that = this;
        //var userInfo =  wx.getStorageSync('userInfo');
        var data = [];
        data['brand_id']= brand_id;
        data['series_id']= series_id;
        data['color_id']= color_id;
        data['motor_id']= motor_id;
        data['cabin_id']= cabin_id;
        data['box_id']= box_id;
        var url = 'clear/search';

      app.request(url,data,'get').then(res=>{ 
        console.log('customer getIndex'); 
         console.log(res);
     
         //this.initChart(res.pie);
         that.setData({   
             'list':res.list,  
             'count':res.count         
             
         });

       //  that.wxUpdate(); //that.wxLogIn();  
      })
      },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})